/*
package com.deer.wms.base.system.web.rocketmq;

import com.alibaba.fastjson.JSONObject;
import com.deer.wms.base.system.finalUtil.BillState;
import com.deer.wms.base.system.finalUtil.BoxInfoState;
import com.deer.wms.base.system.finalUtil.CellInfoState;
import com.deer.wms.base.system.finalUtil.TopicName;
import com.deer.wms.base.system.model.BillInRecord;
import com.deer.wms.base.system.model.bill.*;
import com.deer.wms.base.system.model.box.BoxInfo;
import com.deer.wms.base.system.model.box.BoxItem;
import com.deer.wms.base.system.model.box.BoxItemDto;
import com.deer.wms.base.system.model.task.PickTaskDto;
import com.deer.wms.base.system.model.task.TaskInfo;
import com.deer.wms.base.system.model.task.TaskInfoDto;
import com.deer.wms.base.system.model.ware.CellInfo;
import com.deer.wms.base.system.service.BillInRecordService;
import com.deer.wms.base.system.service.bill.*;
import com.deer.wms.base.system.service.box.BoxInfoService;
import com.deer.wms.base.system.service.box.BoxItemRecordService;
import com.deer.wms.base.system.service.box.IBoxItemService;
import com.deer.wms.base.system.service.task.ITaskInfoService;
import com.deer.wms.base.system.service.task.PickTaskService;
import com.deer.wms.base.system.service.task.TaskInfoHistoryService;
import com.deer.wms.base.system.service.ware.ICellInfoService;
import com.deer.wms.base.system.util.DateUtil;
import com.deer.wms.base.system.util.JedisServiceUtil;
import com.deer.wms.common.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class PushBillOutConsumer implements CommandLineRunner {
    @Autowired
    private BillOutWorkService billOutWorkService;
    @Autowired
    private ITaskInfoService taskInfoService;
    @Autowired
    private BoxItemRecordService boxItemRecordService;
    @Autowired
    private IBoxItemService boxItemService;
    @Autowired
    private BoxInfoService boxInfoService;
    @Autowired
    private ICellInfoService cellInfoService;
    @Autowired
    private IBillOutDetailService billOutDetailService;
    @Autowired
    private PickTaskService pickTaskService;
    @Autowired
    private TaskInfoHistoryService taskInfoHistoryService;

    @Autowired
    private JedisServiceUtil jedisServiceUtil;

    @Autowired
    private IBillOutMasterService billOutMasterService;
//消费者
    @Value("${apache.rocketmq.consumer.pushConsumer}")
    private String pushConsumer;
//NameServer 地址


    @Value("${apache.rocketmq.namesrvAddr}")
    private String namesrvAddr;
//初始化RocketMq的监听信息，渠道信息

    @Value("${apache.rocketmq.consumer.clientIP}")
    private String clientIP;

    @Transactional(rollbackFor=Exception.class)
    public void messageListener(){
        DefaultMQPushConsumer consumer=new DefaultMQPushConsumer("outWarehouse");
        consumer.setNamesrvAddr(namesrvAddr);
        consumer.setClientIP(clientIP);
        try {
           consumer.subscribe(TopicName.TOPIC_BILLOUT, "push");
            consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); // 程序第一次启动从消息队列头获取数据
            consumer.setConsumeMessageBatchMaxSize(1);//可以修改每次消费消息的数量，默认设置是每次消费一条
            //在此监听中消费信息，并返回消费的状态信息
            consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
                // 会把不同的消息分别放置到不同的队列中
                for(Message msg:msgs){
                    Integer taskInfoId= Integer.parseInt(convertMsgToJsonObj(msg).get("taskInfoId").toString());
                    log.info("获得出库taskInfoId:" + taskInfoId + ",开始更新\r\n" +msg.toString());
                    List<TaskInfoDto> taskInfoDtos = taskInfoService.selectTaskInfoByBillOutId(taskInfoId);
                    for (TaskInfoDto taskInfoDto : taskInfoDtos) {
                        //判断该单号下是否全部完成出库
                        List<BillOutDetailDto> byBillId = billOutDetailService.findByBillId(taskInfoDto.getBillId());
                        int ListSize = byBillId.size();
                        int outSize = 0;
                        for (BillOutDetailDto billOutDetailDto : byBillId) {
                            if( null!=billOutDetailDto.getQuantity()&&null!=billOutDetailDto.getQuantityDelivery()){
                                int quantity = billOutDetailDto.getQuantity();
                                int getQuantityDelivery = billOutDetailDto.getQuantityDelivery();
                                if(getQuantityDelivery>=quantity){
                                    outSize++;
                                }
                            }
                        }
                        if(ListSize==outSize){
                            //更新出库单的状态
                            BillOutMaster billOutMaster = new BillOutMaster();
                            billOutMaster.setBillId(taskInfoDto.getBillId());
                            billOutMaster.setState(3);//任务完成的状态
                            int editBillOutRe = billOutMasterService.updateBillOutMaster(billOutMaster);
                        }
                        //将出库成功的出库的库存挪到流水表，然后删除box_item的库存

                        BoxItemDto boxItemByBoxCode = boxItemService.billOutGetBoxItemByBoxCode(taskInfoDto.getBoxCode());
                        Long id = boxItemByBoxCode.getId();
                        int saveBoxItemRe= boxItemRecordService.insertBoxItemRecord(boxItemByBoxCode);
                        int deBoxItemRe = boxItemService.deleteBoxItemById(id);
                        //更新托盘状态
                        BoxInfo boxInfo = new BoxInfo();
                        boxInfo.setBoxCode(taskInfoDto.getBoxCode());
                        boxInfo.setBoxState(BoxInfoState.BOXINFO_GOODS_ZERO);//0-不在货位 1-货位上  2-任务中
                        boxInfo.setHasGoods(BoxInfoState.BOXINFO_GOODS_ZERO);//   0-无货 1-有货
                        boxInfo.setBoxCellId(0);//货位Id
                        int editBoxInfoRe = boxInfoService.updateBoxInfo(boxInfo);
                        //更新货位信息
                        CellInfo cellInfo = new CellInfo();
                        cellInfo.setState(CellInfoState.STATE_ZERO);//状态  ：0-无托盘；1-有货；2-出库中；3-入库中；4故障
                        cellInfo.setCellId(taskInfoDto.getCellId());
                        cellInfo.setItemId(0);
                        int editCellInfoRe =cellInfoService.updateCellInfo(cellInfo);
                        int saveTaskResult= taskInfoHistoryService.saveTaskInfoHistory(taskInfoService.selectTaskInfoById(taskInfoId));//任务完成同步到历史表
                        int deTaskResult =taskInfoService.deleteTaskInfoById(taskInfoId);
                        PickTaskDto pickTask = new PickTaskDto();
                        */
/* pickTask.setCreateTime(billOutDetailDto.getCreateTime());//出库时间*//*

                        pickTask.setBillOutDetailId(taskInfoDto.getBillOutDetailId());
                        pickTask.setWareId(taskInfoDto.getWareId());//仓库
                        pickTask.setPickQuantity(taskInfoDto.getQuantity());//出库数量
                        pickTask.setPickState(4);//1-待下发，2-已下发任务,3-点数任务已完成，4-库存已扣减，5-取消
                        pickTask.setBatch(taskInfoDto.getBatch()); //批次
                        pickTask.setBoxCode(taskInfoDto.getBoxCode());//箱号
                        pickTask.setStowCreateTime(taskInfoDto.getStowCreateTime());
                        int savePickRe = pickTaskService.savePickTask(pickTask);

                        if(saveBoxItemRe==0||deBoxItemRe==0||editBoxInfoRe==0||editCellInfoRe==0||saveTaskResult==0||deTaskResult==0||savePickRe==0){
                            try {
                                log.error("error--出库更新方法执行出错，手动抛异常taskInfoId:" + taskInfoId);
                                throw new Exception("error--出库更新方法执行出错，手动抛异常");
                            } catch (Exception e) {
                                e.printStackTrace();
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            }
                       }else {
                            jedisServiceUtil.cleanMsg(taskInfoDto.getBoxCode());
                            log.info("出库同步结束"+taskInfoId);
                        }

                    }
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            });
            consumer.start();
        } catch (Exception e) {
            log.error("catch--出库更新等出错了，回滚" );
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }


    private JSONObject convertMsgToJsonObj(Message msg) {
        String taskInfoStr = JSONObject.toJSONString(JSONObject.parse(msg.getBody()));
        // String substring = taskInfoStr.substring(taskInfoStr.lastIndexOf("[") + 1, taskInfoStr.lastIndexOf("]"));
        JSONObject obj = (JSONObject) JSONObject.parse(taskInfoStr);
        return obj;
    }

    @Override
    public void run(String... args) throws Exception {
        new Thread(() -> {
            PushBillOutConsumer pushBillOutConsumer =SpringUtils.getBean(PushBillOutConsumer.class);
            pushBillOutConsumer.messageListener();
        }).start();
    }
}
*/
